<!-- identity -->
<template>
  <div class="identity content_box">
    <div class="header_row flex align-center flex-wrap" v-if="hasBtn('id:query')">
      <div class="flex align-center m-r-2">
        <span>身份名称：</span>
        <el-input v-model="searchObj.RoleName" placeholder="请输入内容" clearable></el-input>
      </div>
      <!-- 新增的下拉框 -->
      <div class="flex align-center m-r-2">
        <span>身份类型：</span>
        <el-select v-model="value" placeholder="管家身份">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>
      <!-- <div class="flex align-center m-r-2">
        <span>身份级别：</span>
        <el-select v-model="searchObj.idLevl" placeholder="请选择">
          <el-option
            v-for="item in idOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div> -->
      <!-- <div class="flex align-center m-r-2">
        <span>权益方案：</span>
        <el-select v-model="searchObj.powerModel" placeholder="请选择">
          <el-option
            v-for="item in powerOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div> -->
      <el-button type="primary" @click="handleFilter">查询</el-button>
    </div>
    <div class="m-b-1 neck_box flex align-center space-between">
      <div>
        <el-button v-if="hasBtn('id:add')" type="success" @click="(currentItem = {}), (showForm = true)">添加身份</el-button>
        <!-- <el-button type="danger">批量删除</el-button> -->
        <el-button v-if="hasBtn('id:export')" type="warning" disabled>导出列表</el-button>
      </div>
      <!--<el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchObj.currentPage"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="pageTotal"
      >
      </el-pagination>-->
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
    </div>
    <el-table class="m-b-1" v-loading="listLoading" :data="list" @sort-change="sortChange" border stripe style="width: 100%" :cell-class-name="tranLeft" :header-cell-class-name="tranLeft">
      <el-table-column type="index" label="序号" align="center" fixed>
      </el-table-column>
      <el-table-column prop="SignIcon" label="图标" align="center">
        <template slot-scope="scope">
          <i class="smicon" :class="scope.row.SignIcon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="RoleName" label="身份名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.RoleName }}
        </template>
      </el-table-column>
      <el-table-column prop="PayAmount" label="身份等级金额" align="center">
      </el-table-column>
      <el-table-column prop="Discount" label="活动价" align="center">
      </el-table-column>

      <!-- <el-table-column prop="address" label="达标奖励方案" align="center">
      </el-table-column> -->
      <el-table-column prop="CreateBy" label="创建人" align="center">
      </el-table-column>
      <el-table-column prop="CreateDate" label="创建时间" align="center" sortable width="180">
      </el-table-column>
      <el-table-column prop="UpdateBy" label="编辑人" align="center">
        <template slot-scope="scope">
          <div>
            {{ scope.row.UpdateBy || "-" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="UpdateDate" label="编辑时间" align="center" width="180" sortable>
        <template slot-scope="scope">
          <div>
            {{ scope.row.UpdateDate || "-" }}
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="left" width="430" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="hasBtn('id:edit')" size="mini" @click="(currentItem = $deepClone(scope.row)), (showForm = true)" type="primary">编辑</el-button>

          <!-- <el-button
            size="mini"
            @click="
              (currentMenuItem = $deepClone(scope.row)),
                (platFormType = 'APP'),
                (showMenuForm = true)
            "
            >分配APP菜单</el-button
          > -->
          <el-button v-if="hasBtn('id:delete')" size="mini" type="danger" @click="$delConfirm(() => del(scope.row.Id))">删除</el-button>

          <el-popover v-if="scope.row.Institution" placement="bottom" width="200" trigger="click">
            <el-button style="margin: 0 10px" size="mini" type="success" slot="reference">权益</el-button>
            <el-button v-if="hasBtn('id:stra') && !scope.row.SystemRole" size="mini" type="success" @click="handlePowerEdit(scope.$index, scope.row)">个人权益</el-button>
            <el-button v-if="scope.row.Institution && hasBtn('id:team')" size="mini" type="warning" @click="
                  RoleId = scope.row.Id;
                  showdialogPower = true;
                ">团队权益</el-button>
          </el-popover>
          <el-button v-else-if="hasBtn('id:stra') && !scope.row.SystemRole" size="mini" type="success" @click="handlePowerEdit(scope.$index, scope.row)">权益</el-button>

          <el-button type="info" v-if="hasBtn('id:menu')" size="mini" @click="
              (currentMenuItem = $deepClone(scope.row)),
                (platFormType = 'PC'),
                (showMenuForm = true),
                (ItemRoleName = scope.row.RoleName)
            ">菜单</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogTitle" class="dialog" :visible.sync="dialogVisible" width="800px" v-if="dialogVisible">
      <template>
        <idpower-edit :RoleId="RoleId" :alldisabled2="false"></idpower-edit>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <UserRoleInfoForm :show.sync="showForm" :form-data="currentItem" @finish="getList"></UserRoleInfoForm>
    <RoleMenuForm :platFormType="platFormType" :show.sync="showMenuForm" :form-data="currentMenuItem" :RoleName="ItemRoleName" @finish="getList"></RoleMenuForm>
    <dialog-power :show.sync="showdialogPower" :RoleId="RoleId"></dialog-power>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getUserRole, delUserRole } from '@/api/system/user-role'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  import popStepper from '@/components/popupStepper'
  import idpowerEdit from './components/identityPowerEdit.vue'
  import UserRoleInfoForm from './components/UserRoleInfoForm'
  import RoleMenuForm from './components/RoleMenuForm'
  import dialogPower from './components/dialog_power.vue'
  import { deepClone } from '@/utils'

  export default {
    // import引入的组件需要注入到对象中才能使用
    name: 'identity',
    components: { Pagination, popStepper, idpowerEdit, UserRoleInfoForm, RoleMenuForm, dialogPower },
    data() {
      // 这里存放数据
      return {
        ItemRoleName: '',
        options: [ //下拉框值
          // {
          //   value: 0,
          //   label: '所有身份'
          // },
          {
            value: 1,
            label: '管家身份'
          },
          {
            value: 2,
            label: '系统身份'
          }
        ],
        value: 1, //下拉框显示的值
        RoleId: "",
        showForm: false,
        showdialogPower: false,
        currentItem: {},
        platFormType: 'PC',
        showMenuForm: false,
        currentMenuItem: {},
        dialogType: "add",
        acttips: '基础信息',
        tips: [
          {
            name: '基础信息',
            active: false,
            icon: 'el-icon-user'
          },

        ],
        dialogVisible: false,
        // searchObj: {
        //   idName: '',
        //   idLevl: '',
        //   powerModel: "",
        //   currentPage: 0
        // },
        addObj: {
          idName: '',
          idLevl: ''
        },
        idOptions: [],
        powerOptions: [],
        list: null,
        total: 0,
        listLoading: true,
        searchObjTmpl: {},
        searchObj: {
          RoleName: '',
          idName: '',
          idLevl: '',
          powerModel: "",
        },
        listQuery: {
          PageIndex: 1,
          PageSize: 10,
          IsPaging: true, //是否分页
          SortName: 'CreateDate',
          SortType: false,
          FilterRole: false,
          FilterIns: false

        },
      }
    },
    // 监听属性 类似于data概念
    computed: {
      ...mapGetters([
        'userInfo'
      ]),
      dialogTitle() {
        return this.dialogType == 'add' ? '新增身份' : this.dialogType == 'edit' ? '编辑身份信息' : '身份策略配置'
      },
      calcDialogType() {
        return (this.dialogType == 'add' || this.dialogType == 'edit')
      }
    },
    // 监控data中的数据变化
    watch: {},
    // 生命周期 - 创建完成（可以访问当前this实例）
    created() {
      this.searchObjTmpl = deepClone(this.searchObj)
      this.getList()
    },
    activated() { },
    // 方法集合
    methods: {
      tranLeft({ columnIndex }) {
        if (columnIndex == 9) {
          return 'p-left'
        }
      },
      showUserInfo(type, obj) {
        this.dialogType = type
        this.dialogVisible = true
        switch (type) {
          case 'powerEdit':
            this.RoleId = obj.row.Id
            break
        }
      },
      //策略配置 编辑
      handlePowerEdit(index, row) {
        let obj = { index, row }
        this.showUserInfo('powerEdit', obj)
      },
      resetFilter() {
        this.searchObj = deepClone(this.searchObjTmpl)
        this.handleFilter()
      },
      sortChange(data) {
        const { prop, order } = data
        this.listQuery.SortName = prop
        if (order === 'ascending') {
          this.listQuery.SortType = true
        } else {
          this.listQuery.SortType = false
        }
        this.handleFilter()
      },
      handleFilter() {
        this.listQuery.PageIndex = 1
        this.getList()
      },
      getQuery() {
        const params = {
          ...this.searchObj,
          ...this.listQuery,
        }
        return params
      },
      async getList() {
        this.listLoading = true
        if (this.value == 1) {
          this.listQuery.FilterIns = false
          this.listQuery.FilterRole = true
        } else if (this.value == 2) {
          this.listQuery.FilterIns = true
          this.listQuery.FilterRole = false
        }
        const params = this.getQuery()
        const { Data, Total } = await getUserRole(params)
        console.log(Data)
        this.listLoading = false
        if (!Data) {
          this.list = []
          this.total = 0
          return
        }
        if (this.userInfo.RoleId == '35a6af7f-a555-46a1-9a26-be7c87b0f1c5') {
          this.list = Data
        } else {
          this.list = Data.filter(item => item.RoleId != '35a6af7f-a555-46a1-9a26-be7c87b0f1c5')
        }
        this.total = Total || 0
      },
      async del(id) {
        const { code } = await delUserRole({
          Id: id
        })
        if (code == 200) {
          this.$notify({
            title: '删除成功',
            message: 'success',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }
      },
    } // 如果页面有keep-alive缓存功能，这个函数会触发
  }
</script>
//@import url(); 引入公共css类
<style  scoped>
  .dialog div > .flex > span {
    width: 20%;
    text-align: justify;
    text-align-last: justify;
  }
  .smicon {
    width: 16px;
    height: 16px;
    overflow: hidden;
    display: inline-block;
    margin-right: 10px;
  }
  .p-left {
    padding-left: 15px;
  }
</style>
